﻿package  {
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	public class IRobot extends MovieClip{
		
		private var pContainer:MovieClip;//游戏容器
		
		private var robotInitX:Number=360;
		private var robotInitY:Number=400;
		private var robotMinX:Number=20;
		private var robotMaxX:Number=700;
		private var robotMinY:Number=80;
		private var robotMaxY:Number=460;
		
		private var sideStr:String;
		
		private var speed:Number=10;
		
		private var moveTimer:Timer;
		
		//***
		public function IRobot(pContainer:MovieClip) {
			this.pContainer = pContainer;
			init();
		}
		
		private function init()
		{
			this.x=robotInitX;
			this.y=robotInitY;
			pContainer.addChild(this);
			sideStr="stop";
			
			//timer
			moveTimer=new Timer(30,0);
			moveTimer.addEventListener(TimerEvent.TIMER,timerIng);
			moveTimer.start();
			
			}
	
		public function leftMove(){
			sideStr="left";
		}
		
		public  function rightMove(){
			sideStr="right";
		}
		
		public function stopMove(){
			sideStr="stop";
		}
		
		public function upMove(){
			sideStr="up";
		}
		
		public function downMove(){
			sideStr="down";
		}
		
		private function timerIng(e:TimerEvent):void{
			switch(sideStr){
				case "left":
					this.x-=speed;
					break;
				case "right":
					this.x+=speed;
					break;
				case "up":
					this.y-=speed/2;
					break;
				case "down":
					this.y+=speed;
					break;
				case "stop":
					return;
					break;
			}
			
			if(this.x<=robotMinX){
				this.x=robotMinX;
			}
			
			if(this.x>=robotMaxX){
				this.x=robotMaxX;
			}
			
			if(this.y<=robotMinY){
				this.y=robotMinY;
			}
			
			if(this.y>=robotMaxY){
				this.y=robotMaxY;
			}
		}
	}
	
}